package com.example.administrator.wallet.view.activity.asset;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.administrator.wallet.R;
import com.example.administrator.wallet.base.BaseActivity;
import com.example.administrator.wallet.utils.QRCodeUtils;
import com.example.administrator.wallet.utils.SharedPrefsUtil;

import butterknife.Bind;
import butterknife.OnClick;

public class ReceivableActivity extends BaseActivity {

    @Bind(R.id.mLose)
    ImageView mLose;
    @Bind(R.id.touxiang)
    ImageView touxiang;
    @Bind(R.id.location)
    TextView location;
    @Bind(R.id.ewm)
    ImageView ewm;
    @Bind(R.id.copy)
    Button copy;
    @Bind(R.id.activity_receivable)
    LinearLayout activityReceivable;

    @Override
    public void init() {
        Intent intent = getIntent();
        String datas = intent.getStringExtra("datas");
        location.setText(datas);
        Bitmap bitmap = QRCodeUtils.createQRImage(this.location.getText().toString(), px2dip(this, 460), px2dip(this, 460));
        ewm.setImageBitmap(bitmap);

        getWindow().setStatusBarColor(getResources().getColor(R.color.statu));
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);

        String value = SharedPrefsUtil.getValue(this, "wallet", "head_img", null);
        if(value!=null){
            Glide.with(this).load(value).into(touxiang);
        }
    }


    /**
     * px 转 dp
     * 48px - 16dp
     * 50px - 17dp
     */
    public static int px2dip(Context context, float pxValue) {
        float scale = getScreenDendity(context);
        return (int) ((pxValue / scale) + 0.5f);
    }


    /**
     * 屏幕密度比例
     */
    public static float getScreenDendity(Context context) {
        return context.getResources().getDisplayMetrics().density;//3
    }

    @Override
    public int getLayoutResource() {
        return R.layout.activity_receivable;
    }

    @OnClick({R.id.mLose, R.id.copy})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.mLose:
                finish();
                break;
            case R.id.copy:
                ClipboardManager clipboard = (ClipboardManager)
                        getSystemService(Context.CLIPBOARD_SERVICE);
                CharSequence charSequence = location.getText().toString();
                ClipData clip = ClipData.newPlainText("label",charSequence);
                clipboard .setPrimaryClip(clip);
                Toast.makeText(this, R.string.copy_success, Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
